﻿namespace Autorent.Desktop.Modules.Addresses.Services
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Autorent.Desktop.Model;
    using Autorent.Desktop.Model.AutorentMngrServiceReference;


    public class StreetsRepository : AutorentEntitiesContext, IStreetsRepository
    {
        public ObservableCollection<Street> GetStreets()
        {
            try
            {
                return new ObservableCollection<Street>(Context.Streets);
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void AddStreet(Street street)
        {
            Context.AddToStreets(street);
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void UpdateStreet(Street street)
        {
            Context.UpdateObject(street);
            Context.SaveChanges();
        }

        public void DeleteStreet(Street street)
        {
            try
            {
                Context.DeleteObject(Context.Streets.Where(s => s.Street_ID.Equals(street.Street_ID)));
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }
    }
}
